package 二分算法;

/**
 * https://leetcode.cn/problems/search-insert-position/description/
 * 查找插入位置
 */
public class likou35 {

    // 暴力枚举O(n) 二分是O(logN)
    // 根据题目要求 得出是查找区间的左端点 (右端点也行, 但是要多一步判断)
    // <t 和 >=t. 答案在右边那个区间
    public int searchInsert(int[] nums, int target) {
        int left = 0, right = nums.length - 1;
        while(left <= right) {
            int mid = left + (right - left) / 2;
            if(nums[mid] < target) left = mid + 1;
            else right = mid - 1;
        }
        return left;
    }

}
